package com.iwop.moto.service.impl;
import com.iwop.moto.comm.Page;
import com.iwop.moto.model.MStoreService;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.BaseService;
import com.iwop.moto.service.StoreServieService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author Created by huyuangui on 2016/7/7.
 */
@Service("storeServiceService")
public class StoreServiceServiceImpl extends BaseService implements StoreServieService {
    private static String baseMapper="MStoreServiceListMapper.";
    private static String selectByStoreIdAndStatus="selectByStoreIdAndStatus";
    private static String selectByStoreIdAndStatusTotal="selectByStoreIdAndStatusTotal";

    private static String selectMStoreServiceUserByPrimaryKey="selectMStoreServiceUserByPrimaryKey";
    private static String inserMStoreServiceUser="inserMStoreServiceUser";
    private static String updateMStoreServiceUserByPrimaryKey="updateMStoreServiceUserByPrimaryKey";

    @Override
    public MStoreService findById(String sid) {
        return  getSqlSession().selectOne(baseMapper + selectByPrimaryKey, sid);
    }

    @Override
    public MStoreService save(MStoreService mStoreService) {
        if(mStoreService==null) return null;
        if (StringUtils.isNotEmpty(mStoreService.getSid())){
            MStoreService tempMStoreService=findById(mStoreService.getSid());
            if(tempMStoreService==null){
              getSqlSession().insert(baseMapper+insert,mStoreService);
            }
            getSqlSession().update(baseMapper+updateByPrimaryKey,mStoreService);
            return findById(mStoreService.getSid());
        }
        return null;
    }

    @Override
    public Page queryListByMStoreService(MStoreService mStoreService, Page page) {
        SuperRecordModel superRecordModel=new SuperRecordModel();
        superRecordModel.put("service_id",mStoreService.getSid());
        superRecordModel.put("store_id",mStoreService.getStoreId());
        superRecordModel.put("status",mStoreService.getStatus());
        superRecordModel.put("lat",mStoreService.getLat());
        superRecordModel.put("log",mStoreService.getLog());
        superRecordModel.put("start", page.getStartIndex());
        superRecordModel.put("end",page.getEndIndex());
        int count=getSqlSession().selectOne(baseMapper+selectByStoreIdAndStatusTotal,superRecordModel);
        if(count>0){
            List<SuperRecordModel> list=getSqlSession().selectList(baseMapper+selectByStoreIdAndStatus,superRecordModel);
            page.setList(list);
        }
        return page;
    }

    /***
     * huyuangui 2016-07-22
     * log   经度
     * lat  纬度
     * status  店铺状态
     * start 分页起始
     * end   分页结束
     * @param superRecordModel
     * @param page
     * @return
     */
    @Override
    public Page findStoreByLatLog(SuperRecordModel superRecordModel, Page page) {
        if(StringUtils.isEmpty(superRecordModel.getString("log"))||StringUtils.isEmpty(superRecordModel.getString("lat"))||
                StringUtils.isEmpty(superRecordModel.getString("status"))){
            return  page;
        }
        superRecordModel.put("start", page.getStartIndex());
        superRecordModel.put("end",page.getEndIndex());

        int count=getSqlSession().selectOne("com.iwop.moto.dao.MStoreServiceListMapper.selectByStoreIdAndStatusTotal",superRecordModel);
        if(count>0){
            List<SuperRecordModel> list=getSqlSession().selectList("com.iwop.moto.dao.MStoreServiceListMapper.selectByStoreIdAndStatus",superRecordModel);
            page.setList(list);
        }
        return page;
    }
    /***
     * huyuangui 2016-07-22
     * log   经度
     * lat  纬度
     * status  店铺状态
     * start 分页起始
     * end   分页结束
     * @param superRecordModel
     * @return
     */
    @Override
    public SuperRecordModel saveStoreServerUser(SuperRecordModel superRecordModel) {
        //判断状态不能为空
       /* temp.put("sid", UUID.randomUUID().toString());
        temp.put("service_id",mStoreService.getSid());
        temp.put("to_storeid",tempStore.getString("sid"));
        temp.put("to_userid", tempStore.getString("user_id"));
        temp.put("addtime", DateUtils.DateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));
        temp.put("status","0");*/
        if(StringUtils.isEmpty(superRecordModel.getString("sid"))||StringUtils.isEmpty(superRecordModel.getString("service_id"))||
                StringUtils.isEmpty(superRecordModel.getString("to_storeid"))||StringUtils.isEmpty(superRecordModel.getString("to_userid"))||
                StringUtils.isEmpty(superRecordModel.getString("addtime"))||StringUtils.isEmpty(superRecordModel.getString("status")))
        {
            return null;
        }
        SuperRecordModel temp=getSqlSession().selectOne(baseMapper+selectMStoreServiceUserByPrimaryKey,superRecordModel);
        if(temp==null){
            getSqlSession().insert(baseMapper+inserMStoreServiceUser,superRecordModel);
        }else{
            getSqlSession().update(baseMapper+updateMStoreServiceUserByPrimaryKey,superRecordModel);
        }
        return getSqlSession().selectOne(baseMapper+selectMStoreServiceUserByPrimaryKey,superRecordModel);
    }

    @Override
    public MStoreService workService(MStoreService mStoreService, String userId, String status) {
        if(mStoreService==null)return null;
        //status 1抢服务
        SuperRecordModel superRecordModel=new SuperRecordModel();
        superRecordModel.put("service_id",mStoreService.getSid());
        superRecordModel.put("to_userid",userId);
        SuperRecordModel temp=getSqlSession().selectOne(baseMapper+selectMStoreServiceUserByPrimaryKey,superRecordModel);
        if(temp==null) return null;
        if(status.equals("1")){
            //抢单
            if(temp.getString("status").equals("0") && mStoreService.getStatus().equals("0")){
                temp.put("status","1");
                getSqlSession().update(baseMapper+updateMStoreServiceUserByPrimaryKey,temp);
                mStoreService.setStatus("1");
                mStoreService.setStoreId(temp.getString("store_id"));
                mStoreService.setToUserId(userId);
                getSqlSession().update(baseMapper+updateByPrimaryKey,mStoreService);
                return getSqlSession().selectOne(baseMapper + selectByPrimaryKey, mStoreService.getSid());
            }
        }else if(status.equals("2")){
            //处理完成
            if(temp.getString("status").equals("1") && mStoreService.getStatus().equals("1") && mStoreService.getToUserId().equals(userId)){
                temp.put("status","2");
                getSqlSession().update(baseMapper+updateMStoreServiceUserByPrimaryKey,temp);
                mStoreService.setStatus("2");
                mStoreService.setStoreId(temp.getString("store_id"));
                mStoreService.setToUserId(userId);
                getSqlSession().update(baseMapper + updateByPrimaryKey, mStoreService);
                return getSqlSession().selectOne(baseMapper + selectByPrimaryKey, mStoreService.getSid());
            }
        }else if(status.equals("3")){
            //推送服务
            if(temp.getString("status").equals("2") && mStoreService.getStatus().equals("2") && mStoreService.getToUserId().equals(userId)){
                temp.put("status","3");
                getSqlSession().update(baseMapper+updateMStoreServiceUserByPrimaryKey,temp);
                mStoreService.setStatus("3");
                getSqlSession().update(baseMapper+updateByPrimaryKey,mStoreService);
                return getSqlSession().selectOne(baseMapper+selectByPrimaryKey,mStoreService.getSid());
            }
        }
        return null;
    }

    @Override
    public void saveStoreService(SuperRecordModel SRModel) {
        getSqlSession().insert("MStoreServiceMapper.insertStoreService",SRModel);
    }

    @Override
    public List getAfterSaling(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MStoreServiceListMapper.getAfterSaling", SRModel);
    }

    @Override
    public List<SuperRecordModel> findByServiceOderListStoreId(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MStoreServiceListMapper.selectByServiceOderListStoreId", SRModel);
    }

    @Override
    public SuperRecordModel findServiceBySid(SuperRecordModel superRecordModel) {
        return getSqlSession().selectOne("MStoreServiceMapper.findServiceBySid",superRecordModel);
    }

    @Override
    public void updateStoreService(SuperRecordModel superRecordModel) {
        getSqlSession().update("MStoreServiceMapper.updateStoreService",superRecordModel);
        getSqlSession().update("MStoreServiceListMapper.updateStoreServiceDetail",superRecordModel);
    }

    @Override
    public SuperRecordModel findByServiceOderDetailStoreId(SuperRecordModel superRecordModel) {
        return getSqlSession().selectOne("MStoreServiceListMapper.selectByServiceOderDetailStoreId", superRecordModel);
    }

    @Override
    public List<SuperRecordModel> findByServiceOderH5StoreId(SuperRecordModel superRecordModel) {
        return  getSqlSession().selectList("MStoreServiceListMapper.selectByServiceOderH5StoreId", superRecordModel);
    }

    @Override
    public void addAreaHistory(SuperRecordModel SRModel) {
        getSqlSession().insert("FAreaHistoryMapper.insert",SRModel);
    }

    @Override
    public List<SuperRecordModel> areaHistoryList(SuperRecordModel SRModel) {
        return getSqlSession().selectList("FAreaHistoryMapper.selectAreaByUserId", SRModel);
    }

    @Override
    public List<SuperRecordModel> getStoreList(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MStoreServiceMapper.selectStoreForCity", SRModel);
    }

    @Override
    public SuperRecordModel getServiceNo(SuperRecordModel SRModel) {
        return getSqlSession().selectOne("MStoreServiceMapper.getServiceNo", SRModel);
    }

    @Override
    public List<SuperRecordModel> getStoreOrderPopularList(SuperRecordModel SRModel) {
        return getSqlSession().selectList("MStoreServiceListMapper.getStoreOrderPopularList", SRModel);
    }

    @Override
    public void deleteStoreOrderPopular(SuperRecordModel SRModel) {
         getSqlSession().delete("MStoreServiceListMapper.deleteStoreOrderPopular", SRModel);
         getSqlSession().delete("MStoreServiceListMapper.deleteStoreServiceUser", SRModel);
         getSqlSession().delete("MStoreServiceListMapper.deleteStoreServiceBySid", SRModel);
    }

    @Override
    public SuperRecordModel getServiceBySid(String service_id) {
        return getSqlSession().selectOne("MStoreServiceMapper.getServiceBySid",service_id);
    }

    @Override
    public void deleteServiceStoreUser(String userId) {
        getSqlSession().selectOne("MStoreServiceMapper.deleteServiceStoreUser",userId);
    }
}